package com.example.module.user.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.example.module.user.vo.GetUserByPhoneVO;
import com.example.module.user.vo.UserLoginVO;
import com.example.module.user.form.*;
import com.example.module.user.service.UserService;
import com.example.module.user.vo.GetUserVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author ghy
 * @date 2025/07/21
 * @description 用户控制类
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /*
     * 用户注册
     *
     * @RequestBody userRegisterForm
     * @return void
     * */
    @PostMapping("/register")
    public void UserRegister(@RequestBody UserRegisterForm userRegisterForm) {
        userService.userRegister(userRegisterForm);
    }

    /*
     * 更新用户（根据用户手机号查询）
     *
     * @param userForm
     * @return void
     * */
    @PostMapping("/update")
    @SaCheckLogin
    public void updateUser(@Valid @RequestBody UserUpdateForm userUpdateForm) {
        userService.userUpdate(userUpdateForm);
    }

    /*
     * 删除用户（根据用户ID）
     *
     * @RequestBody userDeleteForm
     * @return void
     * */
    @PostMapping("/delete")
    @SaCheckLogin
    public void deleteUser(@Valid @RequestBody UserDeleteForm userDeleteForm) {
        userService.deleteUser(userDeleteForm);
    }

    /*
     * 用户登录
     *
     * @RequestBody userLoginForm
     * @return ResponseEntity<?>
     * */
    @PostMapping("/login")
    public ResponseEntity<?> userLogin(@Valid @RequestBody UserLoginForm userLoginForm) {
        UserLoginVO user = userService.userLogin(userLoginForm);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.status(401).body("账号或密码错误");
        }
    }

    /*
     * 当前用户查询
     *
     * @return GetUserDO
     * */
    @GetMapping("/info")
    @SaCheckLogin
    public GetUserVO getCurrentUser() {
        return userService.getCurrentUser();
    }

    /*
     * 根据用户手机号查询
     * 用于登录检验是否存在该账号
     *
     * @param getUserByPhoneForm
     * @return GetUserByPhoneVO
     * */
    @GetMapping("/getUserByPhone")
    public GetUserByPhoneVO getUserByPhone(@Valid @RequestBody GetUserByPhoneForm getUserByPhoneForm) {
        return userService.getUserByPhone(getUserByPhoneForm);
    }

    /*
     * 用户注销
     *
     * @return void
     * */
    @GetMapping("/logout")
    @SaCheckLogin
    public void logout() {
        //检查用户是否登录
        userService.logout();
    }
}
